percent = ( (fatGrams * 9) / calories ) * 100 ;
No. This calculation is done with integer arithmetic,
then the result is
converted to double
when it is assigned to percent
.
This is not as accurate as performing the calculation with double
.
Here are the variables and the graphical component each corresponds to:
GUI Component | GUI Method | |
---|---|---|
int calories | JTextField ; | getText() |
int fatGrams | JTextField ; | getText() |
double percent | JTextField ; | setText() |
Each JTextField
will have a JLabel
and the whole GUI
will have a JLabel
at the top to act as a heading.
All this is much as in the previous example.
There is something different about this GUI, though.
The user inputs two values.
The calculation should only be carried out when two values are
available.
With the previous program,
the input JTextField
had a listener registered for it.
When the user typed something and hit enter
,
the listener was "fired" and in turn ran the application.